package org.catdroid.android.farmalona.utils;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.ArrayList;
import java.io.InputStream;

import org.catdroid.android.farmalona.R;
import org.catdroid.android.farmalona.bean.Pharmacy;

import android.content.Context;


/**
 * @author javimarmol
 * This is the wrapper to parse csv to List<Pharmacy>
 */
public class Utils {
	
	public static List<Pharmacy> getListPharmacyFromCSV(Context c) throws FileNotFoundException, IOException{
		InputStream fstream =  c.getResources().openRawResource(R.raw.farmacias2);
		ArrayList<Pharmacy> lp = new ArrayList<Pharmacy>();
		  // Get the object of DataInputStream
		  DataInputStream in = new DataInputStream(fstream);
		  BufferedReader br = new BufferedReader(new InputStreamReader(in));
		  String strLine;
		  //Read File Line By Line
		  while ((strLine = br.readLine()) != null)   {
			String[] tokens = strLine.split(";");
			String type = tokens[2].split(" ")[0];
			if(tokens[0] != "CODI_CAPA" && type.equalsIgnoreCase("Farmacia")){
				Pharmacy p = new Pharmacy();
				p.codiCapa=   tokens[0];
				p.nomCapaCat= tokens[1];
				p.nomCapaCast=tokens[2];
				p.nomCapaAng= tokens[3];
				p.coordX=     tokens[4];
				p.coordY=     tokens[5];
				p.equipament= tokens[6];
				p.telefon=    tokens[7];
				p.adreca=     tokens[8];
				
			}
			
		}
		System.out.println(lp);
		return lp;
	}
}
